package com.ds.shopping_admin_service.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ds.shopping_admin_service.mapper.RoleMapper;
import com.ds.shopping_common.pojo.Role;
import com.ds.shopping_common.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 编写角色服务实现类
 */
@DubboService
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    //新增角色
    @Override
    public int findAdd(Role role) {

        return roleMapper.insert(role);
    }

    //修改角色
    @Override
    public int update(Role role) {

        return roleMapper.updateById(role);
    }

    //删除角色
    @Override
    public void delete(Long rid) {
        //删除角色对应的所有权限
        roleMapper.deletePermissionAllRole(rid);
        //删除角色
        roleMapper.deleteById(rid);

    }

    //通过id查询角色
    @Override
    public Role findById(Long rid) {

        return roleMapper.selectById(rid);
    }

    //查询所有角色
    @Override
    public List<Role> findAll() {

        return roleMapper.selectList(null);
    }

    //分页查询角色
    @Override
    public Page<Role> search(int page, int size) {

        return roleMapper.selectPage(new Page(page,size),null);
    }

    //修改角色权限
    @Override
    public void addPermissionToRole(Long rid, Long[] pids) {
        //1.删除角色对应的的所有权限
        roleMapper.deletePermissionAllRole(rid);
        //2.给角色添加权限
        for (Long pid : pids) {
            roleMapper.addPermissionToRole(rid,pid);
        }

    }
}
